package com.zhuxiaomei.controller;

import com.zhuxiaomei.dto.restaurant.*;
import com.zhuxiaomei.entity.restaurant.RestaurantOrder;
import com.zhuxiaomei.result.Result;
import com.zhuxiaomei.result.ReturnCodeEnum;
import com.zhuxiaomei.service.RestaurantService;
import com.zhuxiaomei.vo.restaurant.RestaurantCategoryVO;
import com.zhuxiaomei.vo.restaurant.RestaurantDishVO;
import com.zhuxiaomei.vo.restaurant.RestaurantOrderVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hxm
 * @date 2024/11/10 15:42
 * @description
 */
@RestController
@RequestMapping("/restaurant")
@Slf4j
public class RestaurantController {

    @Resource
    private RestaurantService restaurantService;

    /**
     * 根据状态获取猪小妹餐厅的菜品类别
     * @return
     */
    @GetMapping("/categories/{status}")
    public Result<List<RestaurantCategoryVO>> getCategoriesByStatus(@PathVariable("status") String status) {
        List<RestaurantCategoryVO> restaurantCategoryVOList = restaurantService.getCategoriesByStatus(status);
        return Result.success(restaurantCategoryVOList);
    }

    /**
     * 根据状态获取猪小妹餐厅的菜品信息
     */
    @GetMapping("/dishes/{status}")
    public Result<List<RestaurantDishVO>> getRestaurantDishesByStatus(@PathVariable("status") String status) {
        List<RestaurantDishVO> restaurantDishVOList = restaurantService.getRestaurantDishesByStatus(status);
        return Result.success(restaurantDishVOList);
    }

    /**
     * 根据id修改菜品状态
     * @param restaurantDishesStatusDTOList
     * @return
     */
    @PutMapping("/changeDishesStatus")
    public Result<String> putChangeDishesStatus(@RequestBody List<RestaurantDishesStatusDTO> restaurantDishesStatusDTOList) {
        Integer row = restaurantService.putChangeDishesStatus(restaurantDishesStatusDTOList);
        if (row > 0) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    /**
     * 获取猪小妹餐厅所有的菜品类别
     */
    @GetMapping("/categories")
    public Result<List<RestaurantCategoryVO>> getAllCategories(){
        List<RestaurantCategoryVO> restaurantCategoryVOList = restaurantService.getAllCategories();
        return Result.success(restaurantCategoryVOList);
    }

    /**
     * 猪小妹餐厅新增菜品
     */
    @PostMapping("/addDish")
    public Result<String> postAddDish(@RequestBody RestaurantDishDTO restaurantDishDTO){
        Integer row = restaurantService.postAddDish(restaurantDishDTO);
        if (row > 0) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    /**
     * 根据id获取猪小妹餐厅的菜品信息
     */
    @GetMapping("/dish/{id}")
    public Result<RestaurantDishVO> getRestaurantDishById(@PathVariable("id") Long id){
        RestaurantDishVO restaurantDishVO = restaurantService.getRestaurantDishById(id);
        return Result.success(restaurantDishVO);
    }

    /**
     * 修改菜品信息
     */
    @PutMapping("/editDish")
    public Result<String> putEditDish(@RequestBody RestaurantDishDTO restaurantDishDTO){
        // 如果 dishImage 字段的值是字符串 "null"，将其设置为 null
        if ("null".equals(restaurantDishDTO.getDishImage())) {
            restaurantDishDTO.setDishImage(null);
        }
        Integer row = restaurantService.putEditDish(restaurantDishDTO);

        if (row > 0) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    /**
     * 根据id删除猪小妹餐厅的菜品
     */
    @DeleteMapping("/deleteDish/{id}")
    public Result<String> deleteRestaurantDish(@PathVariable("id") Long id){
        Integer row = restaurantService.deleteRestaurantDishById(id);
        if (row > 0) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    /**
     * 猪小妹餐厅新增菜品类别
     * @param restaurantCategoryDTO
     * @return
     */
    @PostMapping("/addCategory")
    public Result<String> postAddCategory(@RequestBody RestaurantCategoryDTO restaurantCategoryDTO){
        Integer row = restaurantService.postAddCategory(restaurantCategoryDTO);
        if (row > 0) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    /**
     * 根据状态获取猪小妹餐厅的所有类别，包括其类别下的所有以上下架菜品
     */
    @GetMapping("/categoryAndDishes/{status}")
    public Result<Map<String, List<RestaurantDishVO>>> getCategoryAndDishes(@PathVariable("status") Byte status){
        Map<String, List<RestaurantDishVO>> categoryAndDishes = restaurantService.getCategoryAndDishes(status);

        return Result.success(categoryAndDishes);
    }

    /**
     * 根据菜品类别名称查询类别下是否有在售菜品，返回在售菜品id
     * @param categoryName
     * @return
     */
    @GetMapping("/dishOnSaleByCategoryName/{categoryName}")
    public Result<List<Long>> getDishOnSaleByCategoryName(@PathVariable("categoryName") String categoryName){
        List<Long> dishOnSaleIds = restaurantService.getDishOnSaleByCategoryName(categoryName);
        return Result.success(dishOnSaleIds);
    }
    /**
     * 根据菜品类别名称查询类别下是否有未在售菜品，返回在售菜品id
     * @param categoryName
     * @return
     */
    @GetMapping("/dishOnNoSaleByCategoryName/{categoryName}")
    public Result<List<Long>> getDishOnNoSaleByCategoryName(@PathVariable("categoryName") String categoryName){
        List<Long> dishOnSaleIds = restaurantService.getDishOnNoSaleByCategoryName(categoryName);
        return Result.success(dishOnSaleIds);
    }

    /**
     * 根据status改变菜品类别状态，同时改变该类别下的dish的status。
     * @param status
     * @param restaurantCategoryAndDishIdsDTOList
     * @return
     */
    @PutMapping("/changeCategoryAndDishesStatus/{status}")
    public Result<String> putChangeCategoryAndDishesStatus(@PathVariable("status") Byte status,
                                                           @RequestBody List<RestaurantCategoryAndDishIdsDTO> restaurantCategoryAndDishIdsDTOList){
        Boolean isOk = restaurantService.changeCategoryAndDishesStatus(status, restaurantCategoryAndDishIdsDTOList);
        if (isOk) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    /**
     * 根据菜品类别名称获取菜品类别信息
     * @param categoryName
     * @return
     */
    @GetMapping("/getRestaurantCatetoryInfoByCategoryName/{categoryName}")
    public Result<RestaurantCategoryVO> getRestaurantCatetoryInfoByCategoryName(@PathVariable("categoryName") String categoryName) {
        RestaurantCategoryVO restaurantCategoryVO = restaurantService.getRestaurantCatetoryInfoByCategoryName(categoryName);
        return Result.success(restaurantCategoryVO);
    }

    /**
     * 修改菜品种类信息
     * @param restaurantCategoryDTO
     * @return
     */
    @PutMapping("/putRestaurantCategory")
    public Result<String> putRestaurantCategory(@RequestBody RestaurantCategoryDTO restaurantCategoryDTO) {
        Integer row = restaurantService.putRestaurantCategory(restaurantCategoryDTO);
        if (row > 0) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    @DeleteMapping("/deleteCategoryByName/{categoryName}")
    public Result<String> deleteCategoryByName(@PathVariable("categoryName") String categoryName){
        Integer row = restaurantService.deleteCategoryByName(categoryName);
        if (row > 0) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.fail(ReturnCodeEnum.RC999.getCode(), ReturnCodeEnum.RC999.getMessage());
    }

    @PostMapping("/newOrder")
    public Result<String> newOrder(@RequestBody List<RestaurantOrderDTO> restaurantOrderDTOList){
//        System.out.println(restaurantOrderDTOList);
        String orderNo = restaurantService.newOrder(restaurantOrderDTOList);
        if (orderNo == null){
            return Result.fail(ReturnCodeEnum.NOT_SUFFICIENT_FUNDS.getCode(), ReturnCodeEnum.NOT_SUFFICIENT_FUNDS.getMessage());
        }
        return Result.success(orderNo);
    }

    @GetMapping("/restaurantOrderInfo/{orderNo}")
    public Result<RestaurantOrderVO> getOrderInfoByOrderNo(@PathVariable("orderNo") String orderNo){
        RestaurantOrderVO restaurantOrderVO = restaurantService.getRestaurantOrderInfoByOrderNo(orderNo);
        return Result.success(restaurantOrderVO);
    }

    @GetMapping("/restaurantOrders/{status}")
    public Result<List<RestaurantOrderVO>> getRestaurantOrdersByStatus(@PathVariable("status") Integer status){
        List<RestaurantOrderVO> restaurantOrderVOList = restaurantService.getRestaurantOrderInfo(status);
        return Result.success(restaurantOrderVOList);
    }

    @GetMapping("/restaurantOrdersCount/{status}")
    public Result<Integer> getRestaurantOrdersCountByStatus(@PathVariable("status") Integer status){
        List<RestaurantOrderVO> restaurantOrderVOList = restaurantService.getRestaurantOrderInfo(status);
        Integer size = restaurantOrderVOList.size();
        return Result.success(size);
    }

    @PutMapping("/editRestaurantOrder")
    public Result<String> editRestaurantOrder(@RequestBody RestaurantOrderVO restaurantOrderVO){
        restaurantService.editRestaurantOrder(restaurantOrderVO);
        return Result.success(ReturnCodeEnum.RC200.getCode());
    }
}
